package com.tudouji.framework.task;


import com.tudouji.common.enums.*;
import com.tudouji.common.utils.DateUtils;
import com.tudouji.common.utils.StringUtils;
import com.tudouji.project.bill.domain.*;
import com.tudouji.project.bill.mapper.WhoutformMapper;
import com.tudouji.project.bill.mapper.WhoutformitemMapper;
import com.tudouji.project.bill.service.*;
import com.tudouji.project.copy.domain.*;
import com.tudouji.project.copy.mapper.CopytokdMapper;
import com.tudouji.project.copy.service.*;
import com.tudouji.project.customer.domain.Customer;
import com.tudouji.project.customer.service.ICustomerService;
import com.tudouji.project.organization.domain.Organization;
import com.tudouji.project.organization.domain.Supplier;
import com.tudouji.project.organization.service.IOrganizationService;
import com.tudouji.project.organization.service.ISupplierService;
import com.tudouji.project.product.domain.Innerprice;
import com.tudouji.project.product.domain.Salesspecification;
import com.tudouji.project.product.service.IInnerpriceService;
import com.tudouji.project.product.service.ISalesspecificationService;
import com.tudouji.project.store.domain.Stockunit;
import com.tudouji.project.store.domain.Storehouseroom;
import com.tudouji.project.store.domain.Storeroom;
import com.tudouji.project.store.service.IStockunitService;
import com.tudouji.project.store.service.IStorehouseroomService;
import com.tudouji.project.store.service.IStoreroomService;
import com.tudouji.project.system.domain.SysConfig;
import com.tudouji.project.system.service.ISysConfigService;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import sun.text.bidi.BidiBase;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  处理销售订单的内部结算价格
 */
@Slf4j
@Component
public class InnerPriceProcess {


    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private ISellformService sellformService;

    @Autowired
    private IWhoutformitemService whoutformitemService;

    private List<Salesspecification> salesspecificationList = new ArrayList<Salesspecification>();

    private List<Stockunit> stockunitList = new ArrayList<Stockunit>();

    private List<Innerprice> innerpriceList = new ArrayList<Innerprice>();

    private List<Supplier> supplierList = new ArrayList<Supplier>();
    private List<Customer> customerList = new ArrayList<Customer>();
    @Autowired
    private CopytokdMapper copytokdMapper;
    @Autowired
    private WhoutformMapper whoutformMapper;
    @Autowired
    private WhoutformitemMapper whoutformitemMapper;
    /**
     * 非标品内部结算价（比如7.5/公斤）参数，做为样品、赠送、促销订单的内部结算价
     */
    private BigDecimal innPrice;
    //20240411 add by zhangfu是否启用新的内部价结算逻辑
    private boolean newInnerPriceRule = false;

    /**
     * 内部折扣
     */
    private BigDecimal innDiscount;

    @Autowired
    private IInnerpriceService innerpriceService;

    private String innerSettlePrice_FixedRate,innerSettlePrice_FixAmount;

    public InnerPriceProcess(){

    }

    public void init(List salesspecificationList,List stockunitList,List customerList,List supplierList){
        this.salesspecificationList = salesspecificationList;
        this.stockunitList=stockunitList;
        this.supplierList=supplierList;
        this.customerList=customerList;

        innerpriceList = this.innerpriceService.selectInnerpriceList(new Innerprice());

        this.innerSettlePrice_FixedRate = sysConfigService.selectConfigByKey("innerSettlePrice_FixedRate");
        this.innerSettlePrice_FixAmount = sysConfigService.selectConfigByKey("innerSettlePrice_FixAmount");

        initInnPrice();
        initInnDiscount();
    }

    private void initInnPrice() {
        String price = this.sysConfigService.selectConfigByKey("inner_price");
        if (StringUtils.isNotEmpty(price)) {
            innPrice = new BigDecimal(price);
        }
    }


    private void initInnDiscount() {
        String dis = this.sysConfigService.selectConfigByKey("inner_discount");
        if (StringUtils.isNotEmpty(dis)) {
            innDiscount = new BigDecimal(dis);
        }
    }

    /**
     * 设置copytokditem的amount，对于反向交易，需要设置amount为负数
     * 调用前提：copytokditem.setQty（）已完成调用
     * @param copytokditem
     * @param amount
     */
    private void setCopytokdAmount(Copytokditem copytokditem,BigDecimal amount){
        if(amount == null || copytokditem.getQty() == null){
            return;
        }
        if (amount.compareTo(BigDecimal.ZERO) > 0 &&
                copytokditem.getQty().compareTo(BigDecimal.ZERO) < 0){
            copytokditem.setAmount(amount.multiply(BigDecimal.valueOf(-1)));
            return;
        }
        copytokditem.setAmount(amount);
        return;
    }


    /**
     * 主要用户设置对最终客户的销售价格，在内部结算处理过程中，拆分生成的最后一条数据其实就是
     * 最终生效的对最终客户的销售订单，这个逻辑必须在数据拉取过程中完成，所以，本方法应该是在
     * 数据拉取过程中调用，为了适应在调用过程中也能同时处理内部结构之间结算单价的获取，所以
     * 保留了type=1的逻辑
     * @param copytokd
     * @param copytokditem
     * @param whoutform
     * @param whoutformitem
     * @param sellform
     * @return
     */
    public boolean setPriceAndAmount(Copytokd copytokd, Copytokditem copytokditem,Whoutform whoutform, Whoutformitem whoutformitem, Sellform sellform) {
        boolean result = true;
        if (!copytokd.getKdtype().equals(KDWhoutformTypeEnums.SALE.getCode()) &&
                !copytokd.getKdtype().equals(KDWhinformTypeEnums.PURCHASING.getCode())) {
            return result;
        }
        //20240411 内部结算机制调整，直接从出库单明细表中获取内部结算价
        newInnerPriceRule = newInnerPriceRule();
        //20240527 增加对whoutformitem的判空处理
        if(whoutformitem!=null && newInnerPriceRule)   {
            copytokditem.setPrice(whoutformitem.getStockUnitPrice());
            copytokditem.setInnerPrice(whoutformitem.getInnerPrice());
        }

        //不是销售订单的，就直接赋值出库明细表的单价
        if (whoutform!=null && !whoutform.getOuttype().equals(WhoutformTypeEnums.SALE.getCode())){
            //查一下调拨单的金额在出库明细表里有没有，有的话赋值，没有的话计算一下
            copytokditem.setPrice(whoutformitem.getPrice());
            copytokditem.setAmount(whoutformitem.getPrice().multiply(copytokditem.getKdqty()));
            return result;
        }
        //20231218 处理美团餐饮过来的销售订单
        if(whoutform!=null && whoutform.isExternalChannel()){
            setCopytokdAmount(copytokditem,whoutformitem.getPrice().multiply(copytokditem.getLoadcount()));
            copytokditem.setPrice(copytokditem.getAmount().divide(copytokditem.getKdqty(), 6));
            return  result;
        }
        //是销售订单数据，需要判断当前的客户是否是最终客户，是最终客户的话，直接给销售订单上的单价
        if(sellform==null){
            //sellform不能是null
            sellform = sellformService.selectSellformByCode(copytokd.getCode());
        }
        //销售订单上的客户就是最终客户
        int type;
        if(sellform.getCustomerguid().equals(copytokd.getCustomerid())){
            type=2;    //对最终客户的，则销售订单的单价就是最终的结算单价，销售订单的单价从出库明细表获取
        }else{
            type=1;   //内部客户的，是要获取内部结算价的
        }

        BigDecimal sellPrice = whoutformitem==null?null:whoutformitem.getPrice();
        if (type == 1) {
            result = setInnerSettlePriceAndAmount(copytokd,copytokditem, sellPrice, sellform);
        } else {
            result = setToCustomerPriceAndAmount(copytokditem, whoutformitem, sellform);
        }
        return result;
    }

    /**
     * 设置销售订单的最终销售价格及结算金额
     * @param copytokditem
     * @param whoutformitem
     * @param sellform
     * @return
     */
    private boolean setToCustomerPriceAndAmount(Copytokditem copytokditem, Whoutformitem whoutformitem, Sellform sellform) {
        //获取当前出库明细的物料信息
        List<Salesspecification> salesspecifications = salesspecificationList.stream().filter(s -> copytokditem.getSpguid().equals(s.getGuid())).collect(Collectors.toList());

        if (salesspecifications.size() != 1) {
            copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "设置对外价格及金额时物料信息有误,"+copytokditem.getSpguid());
            return false;
        }

        //获取当前物料的销售单位
        Long materailSfUnitId = salesspecifications.get(0).getSfunitid();
        List<Stockunit> sellunit = this.stockunitList.stream().filter(s -> materailSfUnitId.equals(s.getId())).collect(Collectors.toList());
        if (sellunit.size() == 0) {
            copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "物料销售单位设置有误,for " + materailSfUnitId);
            return false;
        }
        Stockunit saleStockUnit = sellunit.get(0);

        BigDecimal price = BigDecimal.ZERO;
        if(whoutformitem==null){
            //必须获取到销售价格,从对应的销售出库单明细表中获取
            if(StringUtils.isBlank(copytokditem.getWhinoroutitemGuid())&&getWhoutItemId(null,copytokditem)==null){
                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "以销售价进行内部结算但是数据加工过程中无法获取到销售价格,");
                copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                return false;
            }
            whoutformitem = whoutformitemService.selectWhoutformitemById(copytokditem.getWhinoroutitemGuid());
        }
        if (saleStockUnit.getIsUnit().equals(IsWeifhtUnitEnums.NO.getCode())) {
            //物料的销售单位是按件数
            price = whoutformitem.getPrice();
            setCopytokdAmount(copytokditem,price.multiply(copytokditem.getLoadcount()));
//            copytokditem.setAmount(price.multiply(new BigDecimal(whoutformitem.getLoadcount())));
            copytokditem.setPrice(copytokditem.getAmount().divide(copytokditem.getKdqty(), 6));
            return true;
        }

        //销售订单的单价转换为按KG计算的单价，金蝶的重量单位是KG
        price = whoutformitem.getPrice().multiply(sellunit.get(0).getToKg());

        copytokditem.setPrice(price);
        setCopytokdAmount(copytokditem,price.multiply(copytokditem.getKdqty()));
//        copytokditem.setAmount(price.multiply(copytokditem.getKdqty()));

        return true;
    }

    /**
     * 20230402
     * 系统上线前生成的copytokditem数据没有记录whoutformitem的guid，补救措施
     * @param copytokd
     * @param copytokditem
     * @return
     */
    public String getWhoutItemId(Copytokd copytokd,Copytokditem copytokditem){
        if(copytokd==null){
            copytokd= copytokdMapper.selectCopytokdById(copytokditem.getCtkdid());
        }
        if(!copytokd.getCode().startsWith("SF")){
            return null;
        }
        Whoutform whoutform=whoutformMapper.selectWhoutformById(copytokd.getLinkedautoid());
        Whoutformitem wi=new Whoutformitem();
        wi.setWhoutid(whoutform.getGuid());
        wi.setSpguid(copytokditem.getSpguid());
        List<Whoutformitem> whoutformitemList = whoutformitemMapper.selectWhoutformitemList(wi);
        if(whoutformitemList.size()==1){
            copytokditem.setWhinoroutitemGuid(whoutformitemList.get(0).getGuid());
            return whoutformitemList.get(0).getGuid();
        }else{
            wi.setWeight(copytokditem.getQty());
            whoutformitemList = whoutformitemMapper.selectWhoutformitemList(wi);
            if(whoutformitemList.size()==1)
            {
                copytokditem.setWhinoroutitemGuid(whoutformitemList.get(0).getGuid());
                return whoutformitemList.get(0).getGuid();
            }
        }
        return null;
    }

   /**
     * 设置销售订单推送金蝶的内部结算单价及金额
     * 应该在数据加工过程中调用，对于数据拉取处理完成后单价仍然为null的数据做处理
     * 如果是数据拉取过程调用，则调用本方法之前需要调用方明确当前数据一定是两个内部机构之间的交易，并且需要传入whoutformitem
     *
     * @param copytokditem
     * @param sellform
     * @return
     */
    private boolean setInnerSettlePriceAndAmount(Copytokd copytokd,Copytokditem copytokditem,BigDecimal sellPrice, Sellform sellform) {
        //20240411 zhangfu 内部结算价逻辑调整，内部结算价都从销售订单明细表中获取，不再有其他内部计算逻辑
        //如果有内部结算价，取内部结算价，否则，直接去销售价格，数据拉取的时候，销售价格赋值给price，内部结算价格复制个innerPrice
        newInnerPriceRule = newInnerPriceRule();
        if(newInnerPriceRule){
            BigDecimal price = copytokditem.getInnerPrice()==null?copytokditem.getPrice():copytokditem.getInnerPrice();
            //20240527 获取不到内部结算价，可能是5月份内部结算逻辑调整以前的老单子
            if(price == null){
                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有内部结算价," );
                return false;
            }
            copytokditem.setPrice(price);
            setCopytokdAmount(copytokditem,price.multiply(copytokditem.getKdqty()));
            return true;
        }

        //获取当前出库明细的物料信息
        List<Salesspecification> salesspecifications = salesspecificationList.stream().filter(s -> copytokditem.getSpguid().equals(s.getGuid())).collect(Collectors.toList());

        if (salesspecifications.size() != 1) {
            copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "设置内部价格时物料信息有误," + copytokditem.getSpguid());
            return false;
        }

        if(sellform==null){
            //sellform不能是null,通过copytokd获取到sellform
            sellform = sellformService.selectSellformByCode(copytokd.getCode());
        }

        Salesspecification sp=salesspecifications.get(0);
        //物料的内部结算标识 1-内部结算价 2-销售价格 3-固定毛利率 4-固定毛利额
        Integer priceFlag = sp.getInnerSettlePriceFlag();
        if (null == priceFlag){
            //物料没有设置内部结算属性，则不处理
            copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "物料没有设置内部结算属性,");
            copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
            return false;
        }

        int innerSettlePriceFlag = priceFlag;

        //按照内部结算价结算
        if (1 == innerSettlePriceFlag) {
            return getInnprice(copytokd,copytokditem,sp,sellform.getPaytype(),sellform.getIsdiscount()==1?true:false);
        }
        if(sellPrice==null){
            //必须获取到销售价格,从对应的销售出库单明细表中获取
            if(StringUtils.isBlank(copytokditem.getWhinoroutitemGuid())&&getWhoutItemId(copytokd,copytokditem)==null){
                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "以销售价进行内部结算但是数据加工过程中无法获取到销售价格,");
                copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                return false;
            }
            Whoutformitem whoutformitem = whoutformitemService.selectWhoutformitemById(copytokditem.getWhinoroutitemGuid());
            sellPrice=whoutformitem.getPrice();
        }

        //销售价格
        if (2 == innerSettlePriceFlag){
            return setInnerSettlePriceAndAmountForRate(copytokd, copytokditem, sellPrice, sellform, sp, BigDecimal.ZERO, BigDecimal.ZERO);
        }
        //固定毛利率
        if (3 == innerSettlePriceFlag){
            //20230324 新需求 物料层优先
            BigDecimal rate = sp.getInnerSettleFixRate()==null? new BigDecimal(innerSettlePrice_FixedRate):sp.getInnerSettleFixRate();
            return setInnerSettlePriceAndAmountForRate(copytokd, copytokditem, sellPrice, sellform, sp, rate, BigDecimal.ZERO);
        }
        //固定毛利额
        if (4 == innerSettlePriceFlag){
            //20230324 物料层优先
            BigDecimal amount =sp.getInnerSettleFixRateAmt()==null? new BigDecimal(innerSettlePrice_FixAmount):sp.getInnerSettleFixRateAmt();
            return setInnerSettlePriceAndAmountForRate(copytokd, copytokditem, sellPrice, sellform, sp, BigDecimal.ZERO, amount);
        }
        return true;
    }

    /**
     * newInnerPriceRule此参数2024-05-01之后生效
     * @return
     */
    private boolean newInnerPriceRule(){
        try {
            return DateUtils.compare(DateUtils.getNowDate(), DateUtils.parseDate("2024-05-01 00:00:00" , DateUtils.YYYY_MM_DD_HH_MM_SS));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 通过物料内部结算单价Flag计算单价
     * @param copytokditem
     * @param sellform
     * @param sp
     * @return
     */
    private boolean setInnerSettlePriceAndAmountForRate(Copytokd copytokd,Copytokditem copytokditem, BigDecimal sellPrice, Sellform sellform
            , Salesspecification sp, BigDecimal rate, BigDecimal amount) {
        //赠送类的（赠送、样品，促销）等，非标品按参数中的内部价格
        if (!normalSellForm(sellform.getPaytype())) {
            //20231220 按销售价格进行内部结算的情况下，样品订单的内部结算方式：
            //         - 按重量计价的，取系统参数的innerPrice，否则，按件数的，必须设置内部结算价格
            if (sellform.getSettletype() == 1){
                return getInnprice(copytokd,copytokditem,sp,sellform.getPaytype(),sellform.getIsdiscount()==1?true:false);
            }
            if (innPrice == null) {
                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "需要为非标品设置内部结算价参数,");
                copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                return false;
            } else {
                copytokditem.setPrice(innPrice);
                setCopytokdAmount(copytokditem, copytokditem.getPrice().multiply(copytokditem.getKdqty()));
//                copytokditem.setAmount(copytokditem.getPrice().multiply(copytokditem.getKdqty()));
                return true;
            }
        }

        //正常的、非赠送类的订单

        //如果销售订单是折扣订单，必须获取到折扣参数
        if (sellform.getIsdiscount() == 1 && innDiscount == null) {
            copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "订单设置为折扣，但是没有找到折扣参数,");
            copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
            return false;
        }

        //获取当前物料的销售单位

        Long materailSfUnitId = sp.getSfunitid();

        List<Stockunit> sellunit = this.stockunitList.stream().filter(s -> materailSfUnitId.equals(s.getId())).collect(Collectors.toList());
        if (sellunit.size() == 0) {
            copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "物料销售单位设置有误 for ,"+ materailSfUnitId);
            copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
            return false;
        }
        Stockunit saleStockUnit = sellunit.get(0);

        BigDecimal price = BigDecimal.ZERO;
        if (saleStockUnit.getIsUnit().intValue() != 1) {
            //物料的销售单位是按件数，基本没有这种情况，暂时按照以下逻辑处理
            BigDecimal netWeight=sp.getNetweight();
            price = sellPrice.divide(netWeight,6, BigDecimal.ROUND_DOWN);
            price = price.multiply(BigDecimal.ONE.subtract(rate)).subtract(amount);
            //setCopytokdAmount(copytokditem,price.multiply(new BigDecimal(whoutformitem.getLoadcount())));
//            copytokditem.setAmount(price.multiply(new BigDecimal(whoutformitem.getLoadcount())));
            copytokditem.setPrice(price);
            setCopytokdAmount(copytokditem,price.multiply(copytokditem.getKdqty()));
            return true;
        }

        //销售订单的单价转换为按KG计算的单价，金蝶的重量单位是KG
        price = sellPrice.multiply(sellunit.get(0).getToKg());
        price = price.multiply(BigDecimal.ONE.subtract(rate)).subtract(amount);


        //折扣处理
        if (sellform.getIsdiscount() == 1) {
            price = price.multiply(BigDecimal.ONE.subtract(innDiscount));
        }

        copytokditem.setPrice(price);
        setCopytokdAmount(copytokditem,price.multiply(copytokditem.getKdqty()));
//        copytokditem.setAmount(price.multiply(copytokditem.getKdqty()));

        return true;
    }



    /**
     * 根据销售订单的付款类型判断是否需要生成客户信息
     * 赠送、样品、促销等订单不需要客户数据
     *
     * @param payType
     * @return
     */
    private boolean normalSellForm(Integer payType) {
        if (payType == null) return false;
        return !(payType == 4 || payType == 5 || payType == 6);
    }

    /**
     * 获取通过innerPrice表设定的组织之间的内部结算价格
     * @param copytokd
     * @param copytokditem
     * @param salesspecification
     * @param sellFormPayType
     * @param isDiscount
     * @return
     */
    public boolean getInnprice(Copytokd copytokd,Copytokditem copytokditem,
                                  Salesspecification salesspecification,
                                  Integer sellFormPayType,boolean isDiscount) {
        if (!copytokd.getKdtype().equals(KDWhoutformTypeEnums.SALE.getCode()) &&
                !copytokd.getKdtype().equals(KDWhinformTypeEnums.PURCHASING.getCode())) {
            return false;
        }
        //目前，内部价只针对销售订单
        if (!copytokd.getCode().substring(0, 2).equals("SF")){
            return false;
        }
        String purOrgGuid="";
        String sellOrgGuid="";
        BigDecimal matchedInnerPrice=BigDecimal.ZERO;
        boolean matched=false;
        //采购入库单，供应商一定是内部供应商，获取内部供应商的组织guid,作为内部价格表中的销售方,内部价格的采购方就是订单的组织guid
        if (copytokd.getType() == BillTypeEnums.IN.getCode() ){
            List<Supplier> suppliers = supplierList.stream().filter(s -> 3 == s.getType() && copytokd.getSupplierid().equals(s.getId())).collect(Collectors.toList());
            if (suppliers.size() == 1) {
                sellOrgGuid=suppliers.get(0).getOrgguid();
                purOrgGuid=copytokd.getOrgguid();
            }else{
                //没有找到内部供应商信息，写入错误信息后返回
                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "内有找到内部供应商,");
                copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                return false;
            }
        }
        //销售出库单，客户一定是内部客户，获取内部客户对应的组织guid,作为内部价格中的采购方，内部价格的销售方就是订单的组织guid
        if (copytokd.getType() == BillTypeEnums.OUT.getCode()) {
            List<Customer> customers = customerList.stream().filter(c -> 2 == c.getType() && copytokd.getCustomerid().equals(c.getGuid())).collect(Collectors.toList());
            if (customers.size() == 1) {
                purOrgGuid = customers.get(0).getOrgguid();
                sellOrgGuid=copytokd.getOrgguid();
            }else{
                //没有找到内部客户信息，写入错误信息后返回
                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到内部客户,");
                copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                return false;
            }
        }
        //拿到了内部价格中的采购方和销售方，开始获取内部价格
        final String fpurOrgGuid=purOrgGuid;
        final String fsellOrgGuid=sellOrgGuid;
        List<Innerprice> innerprices = innerpriceList.stream().filter(i -> (fsellOrgGuid.equals(i.getOrgguid())) && (fpurOrgGuid.equals(i.getPurorgguid())) && (salesspecification.getGuid().equals(i.getSpguid()) && (i.getEffectivetime().getTime() <= copytokd.getOrderdate().getTime()))).collect(Collectors.toList());
        if(innerprices.size() > 0){
            matchedInnerPrice = innerprices.get(0).getPrice();
            matched=true;
        }
        if(!matched){
            //只找销售组织的内部价，该组织对所有内部组织都是一个内部价格
            innerprices = innerpriceList.stream().filter(i -> (fsellOrgGuid.equals(i.getOrgguid())) && (salesspecification.getGuid().equals(i.getSpguid()) && (i.getEffectivetime().getTime() <= copytokd.getOrderdate().getTime()))).collect(Collectors.toList());
            if(innerprices.size() > 0){
                matchedInnerPrice = innerprices.get(0).getPrice();
                matched=true;
            }
        }
        if(!matched){
            //没有内部价格，设置错误信息后返回
            copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "没有找到内部价格,");
            copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
            return false;
        }

        if (isDiscount && normalSellForm(sellFormPayType)) {
            if (innDiscount == null) {
                copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "当前订单为折扣订单但没有设置折扣参数");
                copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                return false;
            } else {
                copytokditem.setPrice(matchedInnerPrice.multiply(BigDecimal.ONE.subtract(innDiscount)));
                return true;
            }
        } else {
            copytokditem.setPrice(matchedInnerPrice);
        }
        return true;
    }

}
